package StackAndQueue;

public class CreateQueue {
    public static class ListNode{
        ListNode next;
        ListNode prev;
        int val;

        ListNode(int val){
            this.val = val;
        }
    }

    ListNode head;//队头
    ListNode tail;//队尾
    int size = 0;

    public void offer(int e){
        ListNode listNode = new ListNode(e);
        if(head==null){
            head = listNode;
        }else{
            tail.next = listNode;
            listNode.prev = tail;
        }
        tail=listNode;
        size++;
    }

    public Object poll(){
        int value=0;
        if(head==null){
            return null;
        } else if (head==tail) {
            tail=null;
            head=null;
        } else {
            value = head.val;
            head = head.next;
            head.prev.next = null;
            head.prev = null;
        }
        size--;
        return value;
    }

    public Object peek(){
        if(head==null){
            return null;
        }
        return head.val;
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return head==null;
    }
}
